package home

import (
	"github.com/astaxie/beego"
	"log"
	"os"
	//"path/filepath"
	"qinnailin.cn/cloudnote/controllers"
	"qinnailin.cn/cloudnote/models"
	"strconv"
	"strings"
	"time"
)

type HomeController struct {
	controllers.BaseController
}

//文章首页
func (this *HomeController) Index() {
	bean := &models.Group{}
	list := bean.Query()
	log.Println(list)
	this.View("index")
}

//查看一篇文章
func (this *HomeController) Detail() {
	id := this.GetString("id")
	article := &models.Article{Id: id}
	err := article.Get()
	if err != nil {
		log.Println("文章读取失败：" + err.Error())
		this.ToJson(nil, err.Error())
		return
	}
	this.ToJson(article)
}

//编辑文章
func (this *HomeController) Edit() {
	id := this.GetString("id")
	article := &models.Article{Id: id}
	err := article.Get()
	if err != nil {
		log.Println("文章读取失败：" + err.Error())
		this.ToJson(nil, err.Error())
		return
	}
	this.ToJson(article)
}

//编辑文章
func (this *HomeController) DoEdit() {
	log.Println("测试post")
	var (
		Id    = this.GetString("Id")
		Title = this.GetString("Title")
		Body  = this.GetString("Body")
		Gid   = this.GetString("GNum")
	)
	//log.Println(Body)
	var bean *models.Article
	if Id == "" {
		bean = &models.Article{Id: models.NewObjId(), Title: Title, Summary: beego.Html2str(Body),
			Body: Body, IniTime: time.Now(), GNum: Gid}
		id := this.Ctx.GetCookie("usertoken")
		bean.Uid = id
		bean.Add()
	} else {
		bean = &models.Article{Id: Id, Title: Title, Body: Body, Summary: beego.Html2str(Body), GNum: Gid}
		err := bean.Update()
		if err != nil {
			log.Println("更新失败!" + err.Error())
			this.ToJson(nil, err.Error())
			return
		}
	}
	this.ToJson(bean.Id, "编辑成功!")
}

//编辑文章
func (this *HomeController) Articles() {
	article := &models.Article{}
	list := article.QueryAll()
	if list == nil {
		this.ToJson(nil, "列表读取失败！")
		return
	}
	this.ToJson(list)
}

//获取文章标题列表
func (this *HomeController) GetArticleBg() {
	groupid := this.GetString("gid")
	article := &models.Article{}
	list := article.Queryg(groupid)
	if list == nil {
		this.ToJson(nil, "列表读取失败！")
		return
	}
	this.ToJson(list)
}

//获取文章列表（分页）
func (this *HomeController) GetArticles() {
	var index, size int
	var err error
	maxid := this.GetString("maxid")
	groupid := this.GetString("groupid")
	in := this.Input().Get("index")
	sz := this.Input().Get("size")
	if in == "" {
		in = "0"
	}
	if sz == "" {
		sz = "20"
	}
	index, err = strconv.Atoi(in)
	size, err = strconv.Atoi(sz)
	if err != nil {
		this.ToJson(nil, "列表读取失败！")
		return
	}
	article := &models.Article{}
	list, count := article.Query(maxid, groupid, index, size)
	if list == nil {
		this.ToJson(nil, "列表读取失败！")
		return
	}
	this.ToPagerJson(list, count)
}

//获取文章列表（分页）
func (this *HomeController) GetIdTitles() {
	key := this.GetString("keyword")
	article := &models.Article{}
	list := article.Seach(key, 10)
	if list == nil {
		this.ToJson(nil, "列表读取失败！")
		return
	}
	this.ToJson(list)
}

func (this *HomeController) Upload() {
	times := time.Now().Format("2006-01-02")
	dir := this.GetString("dir")
	_, h, err := this.GetFile("imgFile")
	if err != nil {
		log.Println("发生了错误！" + err.Error())
	}
	//log.Println(h.Filename)
	//log.Println(beego.AppPath)
	//maindir := filepath.Abs(path)
	afolder := "/static/uploads/" + dir + "/" + times + "/"
	folder := beego.AppPath + afolder

	err = os.MkdirAll(folder, 0777)
	if err != nil {
		log.Println(err.Error())
	}

	ext := strings.Split(h.Filename, ".")
	filename := models.NewObjId() + "." + ext[1]

	//log.Println("后缀名" + ext[1])
	this.SaveToFile("imgFile", folder+filename)
	this.ToUpLoadJson(afolder+filename, "上传成功！", 0)

}
